package org.custom.spring.boot.transaction.service.transactional;

import org.custom.spring.boot.transaction.async.AsyncCall;
import org.custom.spring.boot.transaction.entity.Transaction;
import org.custom.spring.boot.transaction.mapper.TransactionMapper;
import org.custom.spring.boot.transaction.service.propagation.RequiresNewService;
import org.custom.spring.boot.transaction.utils.ValidUtil;
import org.custom.spring.boot.useless.Result;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 非事务方法调用
 *
 * @author LiKun
 * @date 2021/9/17 10:08
 */
@Service
public class TransactionalWithoutService {
    @Resource
    private AsyncCall asyncCall;
    @Resource
    private AssistService assistService;
    @Resource
    private TransactionMapper transactionMapper;
    @Resource
    private RequiresNewService requiresNewService;

    @Result("B回滚(1)")
    @Description("a外部调用B(抛出异常)")
    public int callTransactionException(String data1, String data2) {
        transactionMapper.insert(Transaction.getInstance(data1));
        return assistService.withTransactionException(data2);
    }

    @Result("无回滚(1)")
    @Description("a(抛出异常)外部调用B")
    public int callExceptionTransaction(String data1, String data2) {
        transactionMapper.insert(Transaction.getInstance(data1));
        assistService.withTransaction(data2);
        return ValidUtil.byZero();
    }

    @Result("B回滚(1)")
    @Description("a外部调用B(NEW&&抛出异常)")
    public int newTransactionException(String data1, String data2) {
        transactionMapper.insert(Transaction.getInstance(data1));
        return requiresNewService.transactionException(data2);
    }

    @Result("B回滚(1)")
    @Description("a异步调用B(抛出异常)")
    public int asyncCallTransactionException(String data1, String data2) throws Exception {
        transactionMapper.insert(Transaction.getInstance(data1));
        return asyncCall.transactionException(data2).get();
    }
}
